package cn.edu.zjicm.wordsnet_d.activity.base;

import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import cn.edu.zjicm.wordsnet_d.R;
import cn.edu.zjicm.wordsnet_d.activity.SearchActivity;
import cn.edu.zjicm.wordsnet_d.datapersistent.AppPreference;
import cn.edu.zjicm.wordsnet_d.util.Utils;
import cn.edu.zjicm.wordsnet_d.view.CircleImageView;
import cn.edu.zjicm.wordsnet_d.view.ShareDialog;

public class BaseMenuActivity extends BaseSlideActivity implements OnClickListener {

	private CircleImageView headPortrait;
	private ImageView searchView;// 次右Button
	private RelativeLayout layout1;
	private RelativeLayout layout2;
	private ImageView shareButton;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setTitleView(R.layout.view_main_title_bar);
		findViews();
		initViews();
		immersionStatusBarForXiaoMi();
	}

	private void findViews() {
		shareButton = (ImageView) findViewById(R.id.share_button);
		searchView = (ImageView) findViewById(R.id.search_view);
		headPortrait = (CircleImageView) findViewById(R.id.head_portrait_main);
		layout1 = (RelativeLayout) findViewById(R.id.title_bar_layout);
		layout2 = (RelativeLayout) findViewById(R.id.title_bar_shadow);
	}

	private void initViews() {
		shareButton.setOnClickListener(this);
		searchView.setOnClickListener(this);
		headPortrait.setOnClickListener(this);
		resetUserInfo();
	}

	/**
	 * 显示titleBar的阴影
	 */
	protected void showTitleBarShadow() {
		layout2.setVisibility(View.VISIBLE);
	}

	public void removeShadow() {
		layout2.setVisibility(View.GONE);
	}

	protected void showGuideForAffix() {
		showTitleBarShadow();
		layout2.setBackgroundResource(R.drawable.guide_in_studing_title_bar);
	}

	protected void removeGuideForAffix() {
		removeShadow();
	}

	@Override
	public void onClick(View v) {
		if (v == searchView) {
			SearchActivity.startActivity(BaseMenuActivity.this);
		} else if (v == shareButton) {
			new ShareDialog(BaseMenuActivity.this, Utils.shareFrom.mainActivity);
		} else if (v == headPortrait) {
			Log.i("点击事件", "点击头像打开侧滑");
			openmSlidingPane();
		}
	}

	@Override
	protected void onResume() {
		super.onResume();
		resetUserInfo();
	}

	private void resetUserInfo() {
		String headPortraitURL = AppPreference.getHeadPortrait(BaseMenuActivity.this);
		if (headPortraitURL != null) {
			Uri imageUri = Uri.parse(headPortraitURL);
			headPortrait.setImageURI(null);
			headPortrait.setImageURI(imageUri);
		} else {
			headPortrait.setImageResource(R.drawable.head_portrait_default);
		}
	}

	private void immersionStatusBarForXiaoMi() {
		boolean flag = Utils.immersionStatusBarForXiaoMi(BaseMenuActivity.this);
		if (flag) {
			immersionBaseStatusBarForXiaoMi();
			layout1.setPadding(0, Utils.changeDipToPx(BaseMenuActivity.this, 20), 0, 0);
		}
	}
}
